package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import client.CcrmApp;
import model.Envelope;
import view.ItemToCatalogGUI;
/**
 * This Class is a Item To Catalog Controller, which extends AbstractTransfer
 * @author Moran
 *
 */
public class ItemToCatalogController extends AbstractTransfer {
	private ItemToCatalogGUI TheView;
	private MarketingController prevComt;
	private ArrayList<Integer> ar;
	private ArrayList<String> arr;
	private ItemToCatalogController currCon;
		
		/**
	 * Constructor function
	 * @param TheView is ItemToCatalogGUI instance
	 * @param tempCon is MarketingController instance which is previous Controller
	 */
	public ItemToCatalogController(ItemToCatalogGUI TheView,MarketingController tempCon ){
		this.TheView = TheView;
		this.prevComt = tempCon;
		currCon = this;
		this.TheView.addbackActionListener(new backBottonListener());
		this.TheView.addAddBottonActionListener(new AddListener());
		this.TheView.addcategoryComboActionListener(new categoryComboListener());
		initcategoryDetails();
		inititemDetails();
	}
	/**
	 * Initialize item combobox
	 */
	public void initcategoryDetails(){
          arr = new ArrayList<String>();
		Envelope ev1 = new Envelope(arr,"get all categoryz");
		CcrmApp.clien.setCurrObj(currCon);
		sendToServer(ev1);
	}
	public void inittypeDetails(String Cat){
        arr = new ArrayList<String>();
        arr.add(Cat);
		Envelope ev1 = new Envelope(arr,"get all types in gategory");
		CcrmApp.clien.setCurrObj(currCon);
		sendToServer(ev1);
	}
	public void inititemDetails(){
        ar = new ArrayList<Integer>();
		Envelope ev1 = new Envelope(ar,"get items2");
		CcrmApp.clien.setCurrObj(currCon);
		sendToServer(ev1);
	}
	/**
	*Inner class that handles when Button back Pressed, implements ActiontListener
	* @author Moran
	*/
	class backBottonListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			TheView.dispose();
			prevComt.MarkGui.setVisible(true);		
		}
	}
	/**
	*Inner class that handles when category combo is Pressed, implements ActiontListener
	*to update the type combo box
	* @author Moran
	*/
	class categoryComboListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
			String str=new String(TheView.getCategory());
			inittypeDetails(str);
		}
	}
	/**
	*Inner class that handles when Button add Pressed from the first panel, implements ActiontListener
	* @author Moran
	*/
	class AddListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {
				String[] temp = null;
				try{
					if (TheView.getItem().contains(": "))
						temp = TheView.getItem().split(": ");
					ArrayList<String> str = new ArrayList<String>();
					str.add(TheView.getCategory());
					str.add(TheView.gettype());
					str.add(temp[1]);
					Envelope ev1 = new Envelope(str,"add Item to catalog");
					CcrmApp.clien.setCurrObj(currCon);
					sendToServer(ev1);
			  		CcrmApp.clien.setCurrObj(currCon);//send to server
				}catch(Exception e3){
					System.out.println("illigal input!");
					TheView.warningMas("illigle input error");
			}	
		}
	}
	/**
	 * handle DB Result
	 * @param Object message
	 */
	 @SuppressWarnings("unchecked")
	 @Override
	public void handleDBResult(Object message) {
		 Envelope ev = (Envelope)message;
		 if(ev.getTask().equals("get items2")){//Initialize item combo box
		  if(ev.getMess().equals("no items")){
			  TheView.warningMas("no itrms in DB");
		  }
		  else{
			for(int i = 0; i < ((ArrayList<Integer>)ev.getObject()).size() ; i++){
				String str = "Item ID: " + Integer.toString(((ArrayList<Integer>)ev.getObject()).get(i));
				TheView.getItemCombo().addItem(str);
				}
		    }
		 }
		 if(ev.getTask().equals("get all categoryz")){//Initialize category combo box
			  if(ev.getMess().equals("not found all categoryz")){
				  TheView.warningMas("no categoryz in DB");
			  }
			  else{

				for(int i = 0; i < ((ArrayList<String>)ev.getObject()).size() ; i++){
					String str = ((ArrayList<String>)ev.getObject()).get(i);
					TheView.getCategoryCombo().addItem(str);
					}
			    }
			 }
		 
		 if(ev.getTask().equals("get all types in gategory")){//Initialize type combo box
			  if(ev.getMess().equals("not found types in gategory")){
				  TheView.warningMas("no types in this category");
			  }
			  else{
				  TheView.gettypeCombo().removeAllItems();
				for(int i = 0; i < ((ArrayList<String>)ev.getObject()).size() ; i++){
					String str = ((ArrayList<String>)ev.getObject()).get(i);
					TheView.gettypeCombo().addItem(str);
					}
			    }
			 }
		 if(ev.getTask().equals("add Item to catalog")){//item added or not
			  if(ev.getMess().equals("item is already exist in catalog")){
				  TheView.warningMas("item is already exist in catalog");
			  }
			  else{
				  if(ev.getMess().equals("Item added successfully to catalog"))
					  TheView.warningMas("Item added successfully to catalog");
			  
				  else{ 
						  TheView.warningMas("input error");
				  }
			  } 
	  
		 }
	
	 }
}
